package com.example.minefieldkeepercloud.activity.commonpool;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.TextView;

import com.example.minefieldkeepercloud.R;

import com.example.minefieldkeepercloud.adapter.CommonPoolAdapter;


import com.example.minefieldkeepercloud.bean.CommonPoolBean;

import com.example.minefieldkeepercloud.network.HttpUtils;
import com.example.minefieldkeepercloud.network.MyGson;
import com.example.minefieldkeepercloud.network.NetHelperNew;
import com.example.minefieldkeepercloud.util.NetWorkUtil;
import com.example.minefieldkeepercloud.util.ToastUtil;

import com.github.jdsjlzx.interfaces.OnLoadMoreListener;
import com.github.jdsjlzx.interfaces.OnRefreshListener;
import com.github.jdsjlzx.recyclerview.LRecyclerView;
import com.github.jdsjlzx.recyclerview.LRecyclerViewAdapter;
import com.github.jdsjlzx.recyclerview.ProgressStyle;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 常用矿池
 */
public class CommonPoolAc extends AppCompatActivity {
    @BindView(R.id.recyleview)
    LRecyclerView mRecyclerView;
    @BindView(R.id.tv_empty)
    TextView tv_empty;

    private CommonPoolAdapter commonPoolAdapter;
    private List<CommonPoolBean.DataBean> listData;
    private LRecyclerViewAdapter lRecyclerViewAdapter;
    private int num=1;
    private boolean isfresh=false;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ac_commonpool);
        ButterKnife.bind(this);
        initview();
    }

    /**
     * 初始化
     */
    private void initview() {
        EventBus.getDefault().register(this);
        listData=new ArrayList<>();
        commonPoolAdapter=new CommonPoolAdapter(this,listData);
        lRecyclerViewAdapter = new LRecyclerViewAdapter(commonPoolAdapter);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mRecyclerView.setAdapter(lRecyclerViewAdapter);
        mRecyclerView.setRefreshProgressStyle(ProgressStyle.BallSpinFadeLoader); //设置下拉刷新Progress的样式
        // mRecyclerView.setArrowImageView(R.drawable.iconfont_downgrey);  //设置下拉刷新箭头
        //设置头部加载颜色
        mRecyclerView.setHeaderViewColor(R.color.system_color, R.color.system_color, R.color.bg);
//设置底部加载颜色
        mRecyclerView.setFooterViewColor(R.color.system_color, R.color.system_color, android.R.color.white);


        mRecyclerView.setOnRefreshListener(new OnRefreshListener() {//下拉刷新
            @Override
            public void onRefresh() {
                num=1;
                listData.clear();
                lRecyclerViewAdapter.notifyDataSetChanged();
                getNetData();
            }
        });

        mRecyclerView.setLoadMoreEnabled(true);

        mRecyclerView.setOnLoadMoreListener(new OnLoadMoreListener() {//加载更多
            @Override
            public void onLoadMore() {
                num++;
                getNetData();
            }
        });



        mRecyclerView.forceToRefresh();
    }

    private void getNetData()
    {

        if (!NetWorkUtil.check(this)) {

            return;
        }
        NetHelperNew.GetWhiteOrderList(num+"",new HttpUtils.HttpCallback() {
            @Override
            public void onSuccess(String data) {
                CommonPoolBean commonPoolBean = MyGson.getInstance().fromJson(data, CommonPoolBean.class);
                if (commonPoolBean.getType() == 1) {
                    if (commonPoolBean.getData().size() > 0) {
                        tv_empty.setVisibility(View.GONE);
                        listData.addAll(commonPoolBean.getData());
                        commonPoolAdapter.setListData(listData);
                        lRecyclerViewAdapter.notifyDataSetChanged();

                    } else {
                        if(num==1)
                        {
                            tv_empty.setVisibility(View.VISIBLE);
                        }
                        else
                        {
                            mRecyclerView.setNoMore(true);
                        }


                    }

                } else {
                    ToastUtil.showToast(CommonPoolAc.this, commonPoolBean.getContent());
                }
                mRecyclerView.refreshComplete(10);
            }

            @Override
            public void onError(String msg) {
                super.onError(msg);
                mRecyclerView.refreshComplete(10);
                ToastUtil.showToast(CommonPoolAc.this, msg);
            }
        });



    }



    @OnClick({R.id.tv_poolAd,R.id.title_left})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_poolAd:

                break;
            case R.id.title_left:
                      finish();
                break;
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN, sticky = false, priority = 0)
    public void handleData(String str) {

        if (str.equals("10115")) {
            isfresh=true;
        }


    }

    @Override
    protected void onResume() {
        super.onResume();
        if(isfresh)
        {
            num=1;
            listData.clear();
            lRecyclerViewAdapter.notifyDataSetChanged();
            mRecyclerView.forceToRefresh();
            isfresh=false;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
